package com.gf.base.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.MailException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.gf.common.constant.ViewPaths;
import com.gf.common.exception.BusinessException;
import com.gf.common.exception.NotLoginException;
import com.gf.common.exception.NotOperationEexception;
import com.sun.mail.smtp.SMTPAddressFailedException;

/**
 * 处理异常
 * 如：没有登入的情况
 * @author Colonel
 *
 */
public class BasicController {
	
	private Log logger = LogFactory.getLog(BasicController.class); 
	
	
	//对业务异常进行处理
	@ExceptionHandler(BusinessException.class)
	protected String handlerBussinessExceptions(HttpServletRequest request,BusinessException ex) {
		if(ex instanceof NotLoginException){
			logger.warn("e:you must log in first");
			return "redirect:"+ViewPaths.LOGIN_VIEW+ViewPaths.SUFFIX;
		}
		else if(ex instanceof NotOperationEexception){
			logger.warn("e:you have not right to do this");
			return "redirect:"+ViewPaths.INFO_VIEW+ViewPaths.SUFFIX+"?info="+ex.getInfo();
		}
		else{
			logger.error("e:business exception");
			return "redirect:"+ViewPaths.INFO_VIEW+ViewPaths.SUFFIX+"?info="+ex.getInfo();
		}
	
	}
	
	@ExceptionHandler(Exception.class)
	protected String  handlerException(HttpServletRequest request,Exception ex) {
		if(ex instanceof MailException){
			logger.warn("e: send mail error");
		}
		else if(ex instanceof SMTPAddressFailedException){
			logger.warn("e: the mail address is not really exists.");
		}
		
		else if(ex instanceof SQLException){
			logger.warn("e: mysql error");
		}
		
		logger.error("e:business exception");
		return "error";
	}
	
	
	
}
